﻿Imports System.Data.SqlClient
Public Class FrmSuaHoSoCapGiay
    Private Sub NapLoaiDat1()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select kyhieu, tenld from LOAIDAT", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLD.DisplayMember = "kyhieu"
        CmbLD.ValueMember = "kyhieu"
        CmbLD.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDonViHuyen()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TENHUYEN", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbHuyen.DisplayMember = "T_huyen"
        CmbHuyen.ValueMember = "maH"
        CmbHuyen.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiTaiLieu()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTaiLieu", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLoaiTaiLieu.DisplayMember = "TenLoaiTaiLieu"
        CmbLoaiTaiLieu.ValueMember = "maLoaiTaiLieuID"
        CmbLoaiTaiLieu.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapPhong()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMPhong", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        cmbPhong.DisplayMember = "Tenphong"
        cmbPhong.ValueMember = "maPhongID"
        cmbPhong.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapNgan()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMNgan", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbNgan.DisplayMember = "TenNgan"
        CmbNgan.ValueMember = "maNganID"
        CmbNgan.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKe()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMKe", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbKe.DisplayMember = "TenKe"
        CmbKe.ValueMember = "maKeID"
        CmbKe.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKho()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMKho", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbKho.DisplayMember = "TenKho"
        CmbKho.ValueMember = "maKhoID"
        CmbKho.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapTinhTrang()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTinhTrang", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbTinhTrang.DisplayMember = "TenTinhTrang"
        CmbTinhTrang.ValueMember = "maTinhTrangID"
        CmbTinhTrang.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub CmbHuyen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbHuyen.SelectedIndexChanged
        ' load ten xa
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TenXa where mahuyen = '" & CmbHuyen.SelectedValue.ToString & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbXa.DisplayMember = "TenXa"
        CmbXa.ValueMember = "maxa"
        CmbXa.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapVaoTexBox()
        TextSoHoSo.DataBindings.Clear()
        TextSoHoSo.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "SoHoSo")
        TextTenChu.DataBindings.Clear()
        TextTenChu.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "tenchu")
        TextDiaChi.DataBindings.Clear()
        TextDiaChi.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "diachi")
        cmbPhong.DataBindings.Clear()
        cmbPhong.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "Tenphong")
        CmbNgan.DataBindings.Clear()
        CmbNgan.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "TenNgan")
        CmbKe.DataBindings.Clear()
        CmbKe.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "TenKe")
        CmbKho.DataBindings.Clear()
        CmbKho.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "TenKho")
        CmbTinhTrang.DataBindings.Clear()
        CmbTinhTrang.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "TenTinhTrang")
        TextMaVach.DataBindings.Clear()
        TextMaVach.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "MaVach")
        CmbXa.DataBindings.Clear()
        CmbXa.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "tenxa")
        CmbHuyen.DataBindings.Clear()
        CmbHuyen.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "T_huyen")
        NapDataGridViewThuaDat()
    End Sub
    Private Sub Lamtuoi()
        TextSoHoSo.Text = ""
        TextTenChu.Text = ""
        TextDiaChi.Text = ""
        TextSoThua.Text = ""
        TextToBD.Text = ""
        TextDienTich.Text = ""
        TextMaVach.Text = ""
        TextthuaID.Text = ""

    End Sub
    Private Sub NapVaoTexBoxThuaDat()
        'TextSoHoSo.DataBindings.Clear()
        'TextSoHoSo.DataBindings.Add("text", DataGridViewThuaDat.DataSource, "sohoso")
        TextthuaID.DataBindings.Clear()
        TextthuaID.DataBindings.Add("text", DataGridViewThuaDat.DataSource, "thuaid")
        TextSoThua.DataBindings.Clear()
        TextSoThua.DataBindings.Add("Text", DataGridViewThuaDat.DataSource, "thuadat")
        TextToBD.DataBindings.Clear()
        TextToBD.DataBindings.Add("Text", DataGridViewThuaDat.DataSource, "tobando")
        TextDienTich.DataBindings.Clear()
        TextDienTich.DataBindings.Add("Text", DataGridViewThuaDat.DataSource, "Dientich")
        CmbLD.DataBindings.Clear()
        CmbLD.DataBindings.Add("Text", DataGridViewThuaDat.DataSource, "Loaidat")
        CmbLoaiTaiLieu.DataBindings.Clear()
        CmbLoaiTaiLieu.DataBindings.Add("Text", DataGridViewThuaDat.DataSource, "TenLoaitailieu")
    End Sub
    Private Sub Update_TenChu()
        Try
            Dim Sql As String = ""
            Sql = " Update TENCHULANDAU Set " &
                " xaID = '" & CmbXa.SelectedValue.ToString & "', " &
                " Huyenid = '" & CmbHuyen.SelectedValue.ToString & "'," &
                " tenchu = N'" & TextTenChu.Text & "'," &
                " TinhTrangid = '" & CmbTinhTrang.SelectedValue.ToString & "'," &
                " Diachi = N'" & TextDiaChi.Text & "'," &
                " phongid = '" & cmbPhong.SelectedValue.ToString & " ' , " &
                " nganid = '" & CmbNgan.SelectedValue.ToString & "', " &
                " keid = '" & CmbKe.SelectedValue.ToString & "' , " &
                " Khoid = '" & CmbKho.SelectedValue.ToString & "' , " &
                " mavach = '" & TextMaVach.Text & "'" &
                " where SoHoSo = '" & TextSoHoSo.Text & "' "
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
            MsgBox("Sửa tên chủ thành công!")
            NapDataGridViewtenchu()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub Update_ThuaDat()
        Try
            Dim Sql As String = ""
            Sql = " Update THUADATLANDAU Set " &
                " ThuaDat = '" & TextSoThua.Text & "' , " &
                " ToBanDo = '" & TextToBD.Text & "' , " &
                " DienTich = '" & TextDienTich.Text & "' , " &
                " Loaidat = '" & CmbLD.SelectedValue.ToString & "' , " &
                " LoaiTaiLieu = '" & CmbLoaiTaiLieu.SelectedValue.ToString & "'  " &
                " where thuaID = '" & TextthuaID.Text & "' "
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
            MsgBox("Sửa thửa đất thành công!")
            NapDataGridViewThuaDat()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub NapDataGridView()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select SohoSo, TenChu, DiaChi,tt.tentinhtrang, p.tenphong , ng.tenngan, k.tenke, kh.tenkho, mavach, tenxa, T_huyen  " &
                                " From TENCHULANDAU, DMTinhTrang tt, DMPhong p, DMNgan ng, DmKe k, DmKho kh, tenxa x, tenhuyen h" &
                                " where TinhtrangID= tt.matinhtrangid " &
                                " and phongid=p.maphongid " &
                                " and nganid = ng.manganid " &
                                " and keid = k.makeid " &
                                " and khoid = kh.makhoid " &
                                " and xaid = x.maxa " &
                                " and huyenid = '" & CmbHuyen.SelectedValue.ToString & "' " &
                                " and xaid = '" & CmbXa.SelectedValue.ToString & "' " &
                                " and huyenid = h.mah ", ob_cnn) '
            dbAdapter = New SqlDataAdapter(dbComm)
            tbl = New DataTable()
            dbAdapter.Fill(tbl)
        DataGridViewTenChu.DataSource = tbl
        If (tbl.Rows.Count = 0) Then
            MessageBox.Show("Không tìm thấy thông tin !")
        Else

        End If
            ob_cnn.Close()
    End Sub
    Private Sub TimkiemMaVach()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select SohoSo, TenChu, DiaChi,tt.tentinhtrang, p.tenphong , ng.tenngan, k.tenke, kh.tenkho, mavach, tenxa, T_huyen  " &
                                " From TENCHULANDAU, DMTinhTrang tt, DMPhong p, DMNgan ng, DmKe k, DmKho kh, tenxa x, tenhuyen h" &
                                " where TinhtrangID= tt.matinhtrangid " &
                                " and phongid=p.maphongid " &
                                " and nganid = ng.manganid " &
                                " and keid = k.makeid " &
                                " and khoid = kh.makhoid " &
                                " and xaid = x.maxa " &
                                " and huyenid = '" & CmbHuyen.SelectedValue.ToString & "' " &
                                " and xaid = '" & CmbXa.SelectedValue.ToString & "' " &
                                " and huyenid = h.mah " &
                                " and MaVach = '" & TextMaVach.Text & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewTenChu.DataSource = tbl
        ob_cnn.Close()
    End Sub
 
    Private Sub NapDataGridViewtenchu()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select SohoSo, TenChu, DiaChi,tt.tentinhtrang, p.tenphong , ng.tenngan, k.tenke, kh.tenkho, mavach, tenxa, T_huyen  " &
                                " From TENCHULANDAU, DMTinhTrang tt, DMPhong p, DMNgan ng, DmKe k, DmKho kh, tenxa x, tenhuyen h" &
                                " where TinhtrangID= tt.matinhtrangid " &
                                " and phongid=p.maphongid " &
                                " and nganid = ng.manganid " &
                                " and keid = k.makeid " &
                                " and khoid = kh.makhoid " &
                                " and xaid = x.maxa " &
                                " and huyenid = h.mah " &
                                " and TenChu like N'%" & TextTenChu.Text & "%'", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewTenChu.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDataGridViewThuaDat()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select thua.thuaid, thua.sohoso, thua.thuadat, thua.tobando, thua.dientich, thua.loaidat, " &
                                " tl.tenloaitailieu " &
                                " From  THUADATLANDAU thua,  DMTailieu tl  " &
                                 " WHERE thua.sohoso = '" & TextSoHoSo.Text & "' " &
                                 " and thua.loaitailieu = tl.maloaitailieuID ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewThuaDat.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub TimThuaDat()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select thua.thuaid, thua.sohoso, thua.thuadat, thua.tobando, thua.dientich, thua.loaidat, " &
                                " tl.tenloaitailieu, tx.tenxa, th.T_huyen," &
                                " p.tenphong, ng.tenngan, k.tenke, kh.tenkho, thua.mavach, ten.tenchu " &
                                " From TENCHULANDAU ten, THUADATLANDAU thua,  DMTailieu tl,  " &
                                " DMPhong p, DMNgan ng, DMKe k, DMKho kh,  tenxa tx, tenhuyen th  " &
                                " WHERE  thua.tobando = '" & TextToBD.Text & "' and thua.thuadat = '" & TextSoThua.Text & "' " &
                                " and thua.SoHoSo = ten.SoHoSo " &
                                " and thua.loaitailieu = tl.maloaitailieuID " &
                                " and thua.xaid = tx.maxa " &
                                " and thua.huyenid = th.mah " &
                                " and thua.phongID = p.maphongid " &
                                " and thua.nganid = ng.manganid " &
                                " and thua.keid = k.makeid " &
                                " and thua.khoid = kh.makhoid ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewThuaDat.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub QL_HoSoSuaTen()
        Try
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim NoidungSua As String = " Sửa thông tin chủ sử dụng"
            Dim Sql As String = ""
            Sql = " insert into QL_SuaHoSoCapGiay(HoSoCapGiay,Nguoisua, Ngaysua, NoiDungSua ) " &
                " values('" & TextSoHoSo.Text & "', N'" & tennhap & "'," & Format(Now.ToOADate() - 2) & ", N'" & NoidungSua & "' )"
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub QL_HoSoSuaThua()
        Try
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim NoidungSua As String = " Sửa thông tin thửa đất"
            Dim Sql As String = ""
            Sql = " insert into QL_SuaHoSoCapGiay(HoSoCapGiay,Nguoisua, Ngaysua, NoiDungSua ) " &
                " values('" & TextSoHoSo.Text & "', N'" & tennhap & "'," & Format(Now.ToOADate() - 2) & ", N'" & NoidungSua & "' )"
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub QL_XoaHoSoCapGiay()
        Try
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim Sql As String = ""
            Sql = " insert into QL_XoaHoSoCapGiay(HoSoCapGiay,Nguoixoa, Ngayxoa) " &
                " values('" & TextSoHoSo.Text & "', N'" & tennhap & "'," & Format(Now.ToOADate() - 2) & " )"
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub XoaDuLieuTenChu()
        Dim sql As String = ""
        sql = "Delete from TENCHULANDAU where SoHoSo = '" & TextSoHoSo.Text & "'"
        Dim cmd As New SqlCommand(sql, ob_cnn)
        ob_cnn.Open()
        cmd.ExecuteNonQuery()
        ob_cnn.Close()

    End Sub
    Private Sub XoaDuLieuThuaDat()
        Dim sql As String
        sql = "Delete from THUADATLANDAU where SoHoSo = '" & TextSoHoSo.Text & "'"
        Dim cmd As New SqlCommand(sql, ob_cnn)
        ob_cnn.Open()
        cmd.ExecuteNonQuery()
        ob_cnn.Close()

    End Sub
    Private Sub XoaDuLieuMucLuc()
        Dim sql As String
        sql = "Delete from MuclucCapGiay where SoHoSo = '" & TextSoHoSo.Text & "'"
        Dim cmd As New SqlCommand(sql, ob_cnn)
        ob_cnn.Open()
        cmd.ExecuteNonQuery()
        ob_cnn.Close()

    End Sub
    Private Sub ChuyenTenChu()
        Try
            Dim Sql As String = ""
            Sql = " insert into TENCHULANDAUXOA(SoHoSo,TenChu, DiaChi, XaID, HuyenID, TinhTrangID, PhongID, NganID, KeID, KhoID,  MaVach ) " &
                " values('" & TextSoHoSo.Text & "', " &
                " N'" & TextTenChu.Text & "', " &
                " N'" & TextDiaChi.Text & "', " &
                " '" & CmbXa.SelectedValue.ToString & "'," &
                " '" & CmbHuyen.SelectedValue.ToString & "', " &
                " '" & CmbTinhTrang.SelectedValue.ToString & "'," &
                " '" & cmbPhong.SelectedValue.ToString & "'," &
                " '" & CmbNgan.SelectedValue.ToString & "'," &
                " '" & CmbKe.SelectedValue.ToString & "'," &
                " '" & CmbKho.SelectedValue.ToString & "'," &
                " '" & TextMaVach.Text & "')"
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
            QL_XoaHoSoCapGiay()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub Chuyenthuadat()
        Try
            Dim Sql As String = ""
            Sql = " insert into THUADATLANDAUXOA(SoHoSo, ThuaDat, ToBanDo, DienTich,LoaiDat,LoaiTaiLieu, " &
                " XaID, HuyenID ) values ('" & TextSoHoSo.Text & "'," &
                " '" & TextSoThua.Text & "'," &
                " '" & TextToBD.Text & "'," &
                " '" & TextDienTich.Text & "'," &
                " '" & CmbLD.SelectedValue.ToString & "'," &
                " '" & CmbLoaiTaiLieu.SelectedValue.ToString & "'," &
                " '" & CmbXa.SelectedValue.ToString & "'," &
                " '" & CmbHuyen.SelectedValue.ToString & "')"
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub FrmSuaHoSoCapGiay_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NapLoaiTaiLieu()
        NapDonViHuyen()
        NapLoaiDat1()
        NapPhong()
        NapNgan()
        NapKe()
        NapKho()
        NapTinhTrang()
        DataGridViewThuaDat.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewThuaDat.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
        DataGridViewTenChu.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewTenChu.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure

    End Sub

    Private Sub TextTenChu_TextChanged(sender As Object, e As EventArgs) Handles TextTenChu.TextChanged
        'NapDataGridViewtenchu()

    End Sub
    Private Sub DataGridViewTenChu_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewTenChu.CellClick
        NapVaoTexBox()
    End Sub
    Private Sub DataGridViewThuaDat_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewThuaDat.CellClick
        NapVaoTexBoxThuaDat()
    End Sub
    Private Sub ButtonTimKiem_Click(sender As Object, e As EventArgs) Handles ButtonTimKiem.Click
        Lamtuoi()
        NapDataGridView()

    End Sub
    Private Sub ButtonThoat_Click(sender As Object, e As EventArgs) Handles ButtonThoat.Click
        Me.Close()

    End Sub

    Private Sub ButtonSuaTen_Click(sender As Object, e As EventArgs) Handles ButtonSuaTen.Click
        QL_HoSoSuaTen()
        Update_TenChu()
    End Sub

    Private Sub ButtonThuaDat_Click(sender As Object, e As EventArgs) Handles ButtonThuaDat.Click
        QL_HoSoSuaThua()
        Update_ThuaDat()
    End Sub

    Private Sub ButtonXoa_Click(sender As Object, e As EventArgs) Handles ButtonXoa.Click
        Try
            If TextTenChu.Text = "" Then
                MsgBox("Bạn phải chọn chủ sử dụng cần xóa")
            ElseIf MsgBox("Bạn có chắc muốn xóa không ?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                ChuyenTenChu()
                QL_XoaHoSoCapGiay()
                XoaDuLieuTenChu()
                XoaDuLieuThuaDat()
                XoaDuLieuMucLuc()
                Lamtuoi()
                NapDataGridViewThuaDat()
                NapDataGridView()

                MessageBox.Show(" Xóa thông tin thành công ! ")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub
    
    Private Sub DataGridViewThuaDat_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewThuaDat.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewThuaDat.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewThuaDat.RowHeadersWidth < Size.Width + 20 Then DataGridViewThuaDat.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub

    Private Sub DataGridViewTenChu_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewTenChu.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewTenChu.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewTenChu.RowHeadersWidth < Size.Width + 20 Then DataGridViewTenChu.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
    Private Sub TextMaVach_TextChanged(sender As Object, e As EventArgs) Handles TextMaVach.TextChanged
        TimkiemMaVach()
    End Sub
    Private Sub Them_thua_dat()
        Try
            ob_cnn.Open()
            Dim NgayNhap As String
            Dim Sql As String = ""
            Dim cmd As New SqlCommand
            Sql = "SELECT DISTINCT ngaynhap FROM THUADATLANDAU WHERE SoHoSo = " + TextSoHoSo.Text + ""
            cmd = New SqlCommand(Sql, ob_cnn)
            Dim dt As New DataTable
            dt.Load(cmd.ExecuteReader())
            cmd.Dispose()
            NgayNhap = String.Format("{0:MM/dd/yyyy}", dt.Rows(0).Item(0).ToString())
            Sql = " insert into THUADATLANDAU(SoHoSo, ThuaDat, ToBanDo, DienTich,LoaiDat,LoaiTaiLieu, " &
                " XaID, HuyenID, ngaynhap) values ('" & TextSoHoSo.Text & "'," &
                " '" & TextSoThua.Text & "'," &
                " '" & TextToBD.Text & "'," &
                " '" & TextDienTich.Text & "'," &
                " '" & CmbLD.SelectedValue.ToString & "'," &
                " '" & CmbLoaiTaiLieu.SelectedValue.ToString & "'," &
                " '" & CmbXa.SelectedValue.ToString & "'," &
                " '" & CmbHuyen.SelectedValue.ToString & "', " &
                " '" & NgayNhap & "')"
            cmd = New SqlCommand(Sql, ob_cnn)
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub XoaMotThuaDat()
        Dim sql As String
        sql = "Delete from THUADATLANDAU where thuaID = '" & TextthuaID.Text & "' and SoHoSo = " & TextSoHoSo.Text & ""
        Dim cmd As New SqlCommand(sql, ob_cnn)
        ob_cnn.Open()
        cmd.ExecuteNonQuery()
        ob_cnn.Close()

    End Sub
    Private Sub btnThemThua_Click(sender As Object, e As EventArgs) Handles btnThemThua.Click
        If TextTenChu.Text = "" Then
            MessageBox.Show("Tên chủ sử dụng không được rỗng")
            Return
        End If
        If TextDiaChi.Text = "" Then
            MessageBox.Show("Chưa nhập địa chỉ chủ sử dụng đất")
            Return
        End If
        If TextSoHoSo.Text = "" Then
            MessageBox.Show("Số hồ sơ không được rỗng")
            Return
        End If
        'If (checkID(TextSoHoSo.Text) > 0) Then
        '    MsgBox(" Đã trùng số hồ sơ, bạn kiểm tra lại ")
        '    'Exit Sub
        '    Return
        'End If

        If TextToBD.Text = "0" Or TextToBD.Text = "" Then
            MessageBox.Show("Chưa nhập thông tin tờ bản đồ")
            Return
        End If
        If TextToBD.Text < "0" Then
            MessageBox.Show("Tờ bản đồ không được nhỏ hơn 0")
            Return
        End If
        If TextSoThua.Text = "0" Or TextSoThua.Text = "" Then
            MessageBox.Show("Chưa nhập thông tin thửa đất")
            Return
        End If
        If TextSoThua.Text < "0" Then
            MessageBox.Show("Thửa đất phải lớn hơn 0 ")
            Return
        End If
        If TextDienTich.Text <= "0" Then
            MessageBox.Show(" Diện tích không được rỗng hoặc nhỏ hơn 0 ")
            Return
        Else
            Them_thua_dat()
            NapDataGridViewThuaDat()
            ob_cnn.Close()
        End If
    End Sub

    Private Sub btnXoaThua_Click(sender As Object, e As EventArgs) Handles btnXoaThua.Click
        If TextthuaID.Text = "" Then
            MessageBox.Show("Chọn thửa đất để xóa")
            Return
        End If
        If TextSoHoSo.Text = "" Then
            MessageBox.Show("Chọn thửa đất để xóa")
            Return
        End If
        Dim Msg As String
        Msg = MessageBox.Show("Bạn có thực sự muốn xóa không ?", "Lưu ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If Msg = Windows.Forms.DialogResult.Yes Then
            Chuyenthuadat()
            XoaMotThuaDat()
            TextthuaID.Text = ""
            NapDataGridViewThuaDat()
        End If
    End Sub
End Class